package com.lingxin.gps.sdk;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.pisces.core.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GpsDeviceUtil {

	private static Logger logger = LoggerFactory.getLogger(GpsDeviceUtil.class);

	public static boolean isSucc(String response) {
		JSONObject jsonObject = JSONObject.parseObject(response);
		if(!jsonObject.getBoolean("success")){
			logger.error(response);
			throw new BizException(jsonObject.getString("message"));
		}

		return true;
	}

	public static JSONObject data(String response) {
		JSONObject jsonObject = JSONObject.parseObject(response);
		if(!jsonObject.getBoolean("success")){
			logger.error(response);
			throw new BizException(jsonObject.getString("message"));
		}

		return jsonObject.getJSONObject("data");
	}

	public static List<JSONObject> getDeviceList(JSONObject group, boolean loop){
		List<JSONObject> list = new ArrayList<>();
		list.add(group);
		return getDeviceList(list, loop);
	}

	public static JSONArray getDeviceArray(List<JSONObject> groupList, boolean loop){
		JSONArray deviceArray = new JSONArray();
		for(JSONObject group : groupList){
			deviceArray.addAll(group.getJSONArray("deviceList"));
			if(loop && group.getJSONArray("groupList").size() > 0){
				List<JSONObject> childGroupList = JSON.parseArray(group.getString("groupList"), JSONObject.class);
				deviceArray.addAll(getDeviceArray(childGroupList, loop));
			}
		}
		return deviceArray;
	}

	public static List<JSONObject> getDeviceList(List<JSONObject> groupList, boolean loop){
		if(groupList == null){
			return new ArrayList<>();
		}
		JSONArray deviceArray = getDeviceArray(groupList, loop);

		// 临时过滤，正式启动时客户端就不处理这些了
		List<JSONObject> deviceList = JSON.parseArray(deviceArray.toJSONString(), JSONObject.class);
		Iterator<JSONObject> iterator = deviceList.iterator();
		JSONObject device;
		while (iterator.hasNext()){
			device = iterator.next();
			if(StrKit.isBlank(device.getString("coordinate")) || device.getString("coordinate").equals(",") || device.getString("coordinate").equals("0,0")){
				iterator.remove();
				continue;
			}
			String[] coordinate = device.getString("coordinate").split(",");
			if(Double.valueOf(coordinate[0]) > 180 || Double.valueOf(coordinate[1]) > 90){
				iterator.remove();
				continue;
			}

			//device.put("icon", "/static/themes/images/car/car_red.png");
			device.put("icon", "/static/themes/images/device/device_36x38.png");
			device.put("position", transCoordinate(device.getString("coordinate")));
			device.put("coordinate", transCoordinate(device.getString("coordinate")));
		}

		return deviceList;
	}



	public static double[] transCoordinate(String coordinate){
		String[] arr = coordinate.split(",");
		//return new Double[]{Double.valueOf(arr[0]), Double.valueOf(arr[1])};
		return MapsUtils.gps(Double.valueOf(arr[0]), Double.valueOf(arr[1]));
	}

	public static boolean validateCoordinate(String coordinate){
		if(StrKit.isBlank(coordinate) || coordinate.equals(",") || coordinate.equals("0,0")){
			return false;
		}
		String[] coordinateArray = coordinate.split(",");
		if(Double.valueOf(coordinateArray[0]) > 180 || Double.valueOf(coordinateArray[1]) > 90){
			return false;
		}

		return true;
	}

}
